package wre;

import java.io.*;
import java.util.*;

public class RetortTable {
	Hashtable<Integer, String> retortTable;

	/**
	 * Default Constructor
	 */
	public RetortTable() {
		retortTable = new Hashtable<Integer, String>();
	}

	/**
	 * Allows other classes to add a new retort to the retort table
	 * 
	 * @param toAdd
	 */
	public void insert(String toAdd) {
		Integer key = new Integer(toAdd.hashCode());
		if (!retortTable.containsKey(key)) {
			retortTable.put(key, toAdd);
		}
	}

	/**
	 * Returns the retort for the given key
	 * 
	 * @param key
	 * @return
	 */
	public String get(Integer key) {
		return retortTable.get(key);
	}

	/**
	 * This function populates the hash table using the data in the
	 * retortTable.csv file
	 * 
	 * This method assumes the format of the input string is
	 * "'key', 'retort string'"
	 */
	public void loadRetortTable() {
		// This function will load all of the retorts from the database into the
		// hash table
		File loadFile = new File("retortTable.csv");

		DataInputStream dataFIn = null;
		try {
			dataFIn = new DataInputStream(new BufferedInputStream(
					new FileInputStream(loadFile)));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			while (dataFIn.available() != 0) {
				String key = null, retort = null;
				boolean switcher = true;
				String currLine = dataFIn.readLine();
				StringTokenizer tokens = new StringTokenizer(currLine, ",");
				while (tokens.hasMoreTokens()) {
					if (switcher) {
						key = tokens.nextToken().trim();
					} else
						retort = tokens.nextToken().trim();
				}
				retortTable.put(new Integer(key), retort);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * This function saves all of the data in the hash table to the
	 * retortTable.csv file
	 * 
	 * The save output is "'key', 'retort string'\n"
	 */
	public void saveRetortTable() {
		File saveFile = new File("retortTable.csv");
		FileOutputStream fOut;
		DataOutputStream dataFOut = null;
		try {
			dataFOut = new DataOutputStream(new BufferedOutputStream(
					new FileOutputStream(saveFile)));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String toFile;
		// This function will save all of the retorts from the database to the
		// hash table
		Iterator<Integer> retortKeys = retortTable.keySet().iterator();
		while (retortKeys.hasNext()) {
			toFile = new String();
			Integer currKey = retortKeys.next();
			String currRetort = retortTable.get(currKey);
			toFile += currKey.toString() + ", " + currRetort + "\n";
			try {
				dataFOut.writeChars(toFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
